package com.onesports.intelligent.k12.polarlight.service.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.common.utils.Assert;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.ClassMergeRuleDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.ConfigDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.ClassMergeRule;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseGuidePriceRel;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Config;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.ClassMergeRuleVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.ConfigVO;
import com.onesports.intelligent.k12.polarlight.enums.SysConfigTypeEnum;
import com.onesports.intelligent.k12.polarlight.mapper.course.ClassMergeRuleMapper;
import com.onesports.intelligent.k12.polarlight.mapper.sys.ConfigMapper;
import com.onesports.intelligent.k12.polarlight.service.cls.ClassMergeRuleService;
import com.onesports.intelligent.k12.polarlight.service.course.manage.CourseGuidePriceRelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Wu
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ConfigService extends ServiceImpl<ConfigMapper, Config> {

    private final ClassMergeRuleService classMergeRuleService;

    private final CourseGuidePriceRelService courseGuidePriceRelService;

    /**
     * 分班规则列表
     * @return
     */
    public List<ClassMergeRuleVO> listClassMergeRule(){
        List<ClassMergeRule> list = classMergeRuleService.list();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(item-> BeanUtils.copyProperties(item,ClassMergeRuleVO.class)).collect(Collectors.toList());
    }

    public void addClassMergeRule(ClassMergeRuleDTO dto){
        verification(dto);
        ClassMergeRule classMergeRule = BeanUtils.copyProperties(dto,ClassMergeRule.class);
        classMergeRuleService.save(classMergeRule);
    }

    public void editClassMergeRule(ClassMergeRuleDTO dto){
        checkUse(dto.getId());
        verification(dto);
        ClassMergeRule classMergeRule = BeanUtils.copyProperties(dto,ClassMergeRule.class);
        classMergeRuleService.updateById(classMergeRule);
    }

    public void delClassMergeRule(String id){
        checkUse(id);
        classMergeRuleService.removeById(id);
    }

    public void checkUse(String id){
        Assert.isTrue(StringUtils.isNotBlank(id),"ID不能为空");
        Long count = courseGuidePriceRelService.count(new LambdaQueryWrapper<CourseGuidePriceRel>()
                .eq(CourseGuidePriceRel::getMergeRuleId,id));
        if(count>0){
            throw new BusinessException("拼班规则已被使用");
        }
    }

    private void verification(ClassMergeRuleDTO dto){
        if(dto.getMinNum()>dto.getMaxNum()){
            throw new BusinessException("最低人数要求不能大于人数上限");
        }
    }



    /**
     * 查询
     * @return
     */
    public List<ConfigVO> queryList(){
        List<ConfigVO> list = Lists.newArrayList();
        List<Config>  dataList = baseMapper.selectList(new QueryWrapper<>());
        Arrays.stream(SysConfigTypeEnum.values()).forEach(item->{
            ConfigVO configVO = new ConfigVO();
            configVO.setName(item.getText());
            configVO.setType(item.getValue());
            Config config =  dataList.stream().filter(data->data.getType().equals(item.getValue())).findFirst().orElse(null);
            if(config!=null){
                configVO.setValue(config.getValue());
            }else{
                if(SysConfigTypeEnum.H.equals(item)){
                    configVO.setValue("true");
                }
            }
            list.add(configVO);
        });
        return list;
    }

    /**
     * 修改
     * @param dto
     */
    public void updateByType(ConfigDTO dto){
        Config config = baseMapper.selectOne(new LambdaQueryWrapper<Config>().eq(Config::getType,dto.getType()));
        if(null==config){
            config = new Config();
            config.setType(dto.getType());
            config.setName(dto.getName());
            config.setValue(dto.getValue());
            baseMapper.insert(config);
        }else{
            config.setValue(dto.getValue());
            baseMapper.updateById(config);
        }
    }

    public String getValueByType(SysConfigTypeEnum type){
        Config config = baseMapper.selectOne(new LambdaQueryWrapper<Config>().eq(Config::getType,type));
        if(ObjectUtils.isNotEmpty(config)){
            return config.getValue();
        }
        if(SysConfigTypeEnum.H.equals(type)){
            return "true";
        }
        return null;
    }


    public Config getConfig(String type){
        return this.getOne(new LambdaQueryWrapper<Config>()
                .eq(Config::getType,type));
    }
}
